package org.standard.sys.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import org.standard.base.model.BaseRole;
import org.standard.commons.utils.DAO;
import org.standard.commons.utils.FlipPageInfo;
import org.standard.sys.model.SysAsset;
import org.standard.sys.service.SysAssetService;

class SysAssetServiceImpl implements SysAssetService {
	
	private DAO dao;
	

	public DAO getDao() {
		return dao;
	}

	public void setDao(DAO dao) {
		this.dao = dao;
	}

	@Override
	public char bacthDeleteSysAsset(String[] ids) throws Exception {
		// TODO Auto-generated method stub
		try{
			String blockStr = "";
			if (ids == null || ids.length == 0) {
				blockStr = "('')";
			} else {
				blockStr = "(";
				for (int i = 0; i < ids.length - 1; i++) {
					blockStr += "'" + ids[i] + "',";
				}
				blockStr += "'" + ids[ids.length - 1] + "')";
			}
			String hql = "delete from SysAsset where assetId in"+blockStr;
			
			dao.updateBulk(hql);
			return '1';
		}catch (Exception e) {
			return '0';
		}
	}

	@Override
	public char createSysAsset(SysAsset sysAsset) throws Exception {
		// TODO Auto-generated method stub
		char s = '0';
		
		if(sysAsset != null){
			if(StringUtils.isNotBlank(sysAsset.getFuncId()) 
					&& StringUtils.isNotBlank(sysAsset.getAssetName())
					&& StringUtils.isNotBlank(sysAsset.getAssetNameEn())
					&& StringUtils.isNotBlank(sysAsset.getAssetLink())){
				
				dao.save(sysAsset);
				
				s = '1';//成功
			}else if(!StringUtils.isNotBlank(sysAsset.getFuncId())){
				
				s = '2';//请选择功能点
			}else if(!StringUtils.isNotBlank(sysAsset.getAssetName())){
				
				s = '3';//资源名称不能为空
			}else if(!StringUtils.isNotBlank(sysAsset.getAssetNameEn())){
				
				s = '4';//资源英文名称不能为空
			}else if(!StringUtils.isNotBlank(sysAsset.getAssetLink())){
				
				s = '5';//资源链接不能为空
			}
			
		}
		
		return s;
	}

	@Override
	public char deleteSysAsset(SysAsset sysAsset) throws Exception{
		// TODO Auto-generated method stub
		
		SysAsset sysAsset1 = new SysAsset();
		
		
		if(StringUtils.isNotBlank(sysAsset.getAssetId())){
			
			sysAsset1 = (SysAsset)dao.find(SysAsset.class, sysAsset.getAssetId());
			
			String assetId = sysAsset1.getAssetId();
			
			dao.delete(sysAsset1);
			
			
			
			List<BaseRole> roles = dao.find("from BaseRole where 1=1 order by roleOrder");
			
			for(BaseRole role:roles){
				
				String assetIdGroup = role.getAssetIdGroup();
				
				if(StringUtils.isNotBlank(assetIdGroup) && assetIdGroup.indexOf(assetId) != -1){
					
					String assetIdGroup1 = assetIdGroup.replace(assetId+",", "");
					
					role.setAssetIdGroup(assetIdGroup1);
					
					dao.update(role);
				}
			}
			
			return '1';
		}else{
			
			return '0';
		}
	}

	@Override
	public SysAsset findByAssetId(String assetId) {
		// TODO Auto-generated method stub
		SysAsset sysAsset = new SysAsset();
		
		if(StringUtils.isNotBlank(assetId)){
			
			sysAsset = (SysAsset)dao.find(SysAsset.class, assetId);
		}
		
		return sysAsset;
	}

	@Override
	public FlipPageInfo listSysAsset(FlipPageInfo fpi) {
		// TODO Auto-generated method stub
		try{
			FlipPageInfo pageData = new FlipPageInfo();
			String hql = "select func.funcName,asset  from SysAsset asset,SysFunc func where func.funcId = asset.funcId  order by asset.assetOrder";
			
			pageData = dao.findWithPaging(hql, fpi);
			return pageData;
		}catch (Exception e) {
			return new FlipPageInfo();
		}
	}

	@Override
	public char modifySysAsset(SysAsset sysAsset) throws Exception {
		// TODO Auto-generated method stub
		char s = '0';
		
		if(sysAsset != null && StringUtils.isNotBlank(sysAsset.getAssetId())){
			if(StringUtils.isNotBlank(sysAsset.getFuncId()) 
					&& StringUtils.isNotBlank(sysAsset.getAssetName())
					&& StringUtils.isNotBlank(sysAsset.getAssetNameEn())
					&& StringUtils.isNotBlank(sysAsset.getAssetLink())){
				
				SysAsset sysAsset1 = (SysAsset)dao.find(SysAsset.class, sysAsset.getAssetId());
				
				sysAsset1.setAssetName(sysAsset.getAssetName());
				sysAsset1.setAssetNameEn(sysAsset.getAssetNameEn());
				sysAsset1.setAssetOrder(sysAsset.getAssetOrder());
				sysAsset1.setAssetLink(sysAsset.getAssetLink());
				sysAsset1.setFuncId(sysAsset.getFuncId());
				
				dao.update(sysAsset1);
				
				s = '1';//成功
			}else if(!StringUtils.isNotBlank(sysAsset.getFuncId())){
				
				s = '2';//请选择功能点
			}else if(!StringUtils.isNotBlank(sysAsset.getAssetName())){
				
				s = '3';//资源名称不能为空
			}else if(!StringUtils.isNotBlank(sysAsset.getAssetNameEn())){
				
				s = '4';//资源英文名称不能为空
			}else if(!StringUtils.isNotBlank(sysAsset.getAssetLink())){
				
				s = '5';//资源链接不能为空
			}
			
		}
		
		return s;
	}

	@Override
	public List listSysAsset() {
		// TODO Auto-generated method stub
		try{
			String hql = "select func.funcName,asset  from SysAsset asset,SysFunc func where func.funcId = asset.funcId  order by asset.assetOrder";
			
			List list = dao.find(hql);
			return list;
			
		}catch (Exception e) {
			return new ArrayList();
		}
	}
	
	

}
